Skip to content

android 监听应用进入前后台

插件 ID: 23767
来源: https://ext.dcloud.net.cn/plugin?id=23767
android 通过UsageStatsManager 监听系统所有应用 进入前台后台


更新记录

                                                    1.0.0(2025-06-06)

初始化

平台兼容性

uni-app

| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 | | | √ | √ |

  • |
  • |
  • |
  • | 4.4 |
  • |
  • | | | 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 | | |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • |
  • | |

uni-app x

| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 | | |

  • |
  • | 5.0 |
  • |
  • |
  • | |

xtf-usage

Android监听系统所有应用进入前后台

uniapp

<template>
    <view class="content">
        <button style="margin: 10rpx;" type="primary" @click="onClick(0)">检测是否有使用记录权限</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(1)">申请使用记录权限</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(2)">开启监听手机前后台应用</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(3)">取消监听手机前后台应用</button>
    </view>
</template>
<script>
    import {hasUsageStatsPermission,requestUsageStatsPermission,appListener,stopAppListener} from "@/uni_modules/xtf-usage"
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {
        },
        methods: {
            onClick(id){
                var that=this;
                if(id==0){
                    uni.showToast({
                        icon:"none",
                        title:""+hasUsageStatsPermission()
                    })
                }else if(id==1){
                    requestUsageStatsPermission();
                }else if(id==2){
                    appListener(function(b,pkg){
                        console.log(b,pkg);
                    })
                }else if(id==3){
                    stopAppListener();
                }else if(id==4){
                }
            },
        }
    }
</script>
<style>
    .logo {
        height: 100px;
        width: 100px;
        margin: 100px auto 25px auto;
    }
    .title {
        font-size: 18px;
        color: #8f8f94;
    text-align: center;
    }
</style>

uniappx

<template>
    <view class="content">
        <button style="margin: 10rpx;" type="primary" @click="onClick(0)">检测是否有使用记录权限</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(1)">申请使用记录权限</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(2)">开启监听手机前后台应用</button>
        <button style="margin: 10rpx;" type="primary" @click="onClick(3)">取消监听手机前后台应用</button>
    </view>
</template>
<script>
    import {hasUsageStatsPermission,requestUsageStatsPermission,appListener,stopAppListener} from "@/uni_modules/xtf-usage"
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {
        },
        methods: {
            onClick(id:number){
                var that=this;
                if(id==0){
                    uni.showToast({
                        icon:"none",
                        title:""+hasUsageStatsPermission()
                    })
                }else if(id==1){
                    requestUsageStatsPermission();
                }else if(id==2){
                    appListener(function(b:boolean,pkg:string){
                        console.log(b,pkg);
                    })
                }else if(id==3){
                    stopAppListener();
                }else if(id==4){
                }
            },
        }
    }
</script>
<style>
    .logo {
        height: 100px;
        width: 100px;
        margin: 100px auto 25px auto;
    }
    .title {
        font-size: 18px;
        color: #8f8f94;
    text-align: center;
    }
</style>

开发文档

UTS 语法UTS API插件UTS uni-app兼容模式组件UTS 标准模式组件Hello UTS

MIT Licensed